package com.ronghuanet.datasync.listener;

import com.alibaba.fastjson.JSONObject;
import com.ronghuanet.basic.constants.BaseConstants;
import com.ronghuanet.car.domain.CarType;
import com.ronghuanet.datasync.dto.CanalSynDto;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "example",selectorExpression="*",consumerGroup = "queue_group_test")
public class CarTypeCacheDataDelListener implements RocketMQListener<MessageExt> {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String msg = new String(body, CharsetUtil.UTF_8);
        //log.info("接收到消息：{}", msg);

        CanalSynDto<CarType> canalSynDto = JSONObject
                .parseObject(msg, CanalSynDto.class);
        String type = canalSynDto.getType();
        if ("UPDATE".equals(type)||"INSERT".equals(type)||"DELETE".equals(type))
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            redisTemplate.delete(BaseConstants.CAR_TYPE_TREE_IN_REDIS);
            redisTemplate.delete(BaseConstants.CAR_TYPE_TREE_IN_REDIS+"_bak");
            log.error("fhhfhfhfhfh。。。。。。。。。。。。。。。。。。。。。");
        }

    }

}